package com.example.demo.service;

import com.example.demo.entity.Product;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class ProductService {
    //模拟todo信息的存储
    private Map<Integer, Product> Products =
            Collections.synchronizedMap(new HashMap<Integer,Product>());

    ProductService(){
        Product p=new Product();
        p.setPrice(10);
        p.setName("ww");
        p.setId(1);
        Products.put(p.getId(),p);
        Product p1=new Product();
        p1.setPrice(15);
        p1.setName("hh");
        p1.setId(2);
        Products.put(p1.getId(),p1);
    }
    public Product addProduct(Product p){
        Products.put(p.getId(),p);
        return p;
    }


    public Product getProduct(int id){
        return Products.get(id);
    }

    //根据名字找产品
    public List<Product> findProduct (){
            List<Product> result  = new ArrayList<Product>();
        for(Product c : Products.values()){

            result.add(c);
        }
        return result;
    }

    public void updateProduct(int id, Product p){
        Product temp = Products.get(id);
        temp.setName(p.getName());
        temp.setPrice(p.getPrice());
        Products.put(id, temp);
    }

    public void deleteProduct(int id){
        Products.remove(id);
    }
}
